package com.osard.test_record;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.osard.screen.event.BaseScreenRecordEvent;
import com.osard.screen.event.ScreenRecordSuccessEvent;
import com.osard.screen.utils.ScreenCaptureUtils;
import com.osard.screen.utils.ScreenRecordUtils;
import com.osard.test_record.databinding.ActivityMainBinding;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private final static String TAG = MainActivity.class.getSimpleName();

    boolean record;
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setClick(this);

        EventBus.getDefault().register(this);

        binding.photoView.setImageBitmap(binding.surface, 200, 200);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(BaseScreenRecordEvent event) {
        Log.w(TAG, event.getClass().getName());
        if (event instanceof ScreenRecordSuccessEvent) {
            ScreenRecordSuccessEvent srse = (ScreenRecordSuccessEvent) event;
            Log.w(TAG, srse.getVideoPathName());

//            new File(srse.getVideoPathName()).renameTo(new File(
//                    getExternalFilesDir(Environment.DIRECTORY_MOVIES).getAbsolutePath() +
//                            File.separator + System.currentTimeMillis() + ".mp4"));

        }
    }

    @Override
    public void onClick(View v) {
        if (v.equals(binding.record)) {
            if (record) {
                record = false;
                binding.record.setText("开始录制");

                ScreenRecordUtils.stop();
            } else {
                record = true;
                binding.record.setText("结束录制");

                ScreenRecordUtils.start("/storage/emulated/0/Android/data/com.osard.test_record/files/Moviesss/", "111_xxx.mp4");
            }
        } else if (v.equals(binding.jp)) {
            ScreenCaptureUtils.createInstance()
//                    .setImageName("1.png")
                    .setView(this)
                    .saveBitmapToJpg(100);

//            ImageToPdfUtils.createInstance()
//                    .setBitmaps(binding.surface.palette.screenShot())
//                    .save(getExternalFilesDir("") + File.separator + System.currentTimeMillis() + ".pdf");
        }
    }


}